import os
import time
import logging
from logging.handlers import RotatingFileHandler


class MyLog:
    def __init__(self, logpath, loggername,
                 pre=time.strftime('%Y-%m-%d', time.localtime(time.time())), lavel=logging.DEBUG):
        if not os.path.exists(logpath):
            os.makedirs(logpath)
        log_path = logpath + str(pre) + '.log'
        self.logger = logging.getLogger(loggername)
        self.logger.setLevel(lavel)
        filehandler = RotatingFileHandler(log_path, mode='a', maxBytes=1024 * 100)
        filehandler.setLevel(lavel)
        if not self.logger.hasHandlers():
            consolehandler = logging.StreamHandler()
            consolehandler.setLevel(lavel)
            formater = logging.Formatter('[%(levelname)s]%(asctime)s : %(message)s')
            filehandler.setFormatter(formater)
            consolehandler.setFormatter(formater)
            self.logger.addHandler(filehandler)
            self.logger.addHandler(consolehandler)
        # self.logger.info(f'{loggername} logging Start.')

    def getlog(self):
        return self.logger
